// components/goodsListModal/goodsListModal.js
const defaultData = {
    style: 1
};

Component({
    /**
   * 组件的属性列表
   */
    options: {
        addGlobalClass: true
    },
    properties: {
        goodsListStyle: {
            type: JSON,
            observer(newVal, oldVal, changPath) {
                var comStyle = this.data.comStyle;
                this.setData({
                    comStyle: {
                        ...comStyle,
                        ...newVal
                    }
                });
            }
        },
        goodsListData: {
            type: Array,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    listData: newVal
                });
            }
        },
        deliveryWay: {
            type: Number,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    deliveryWay: newVal
                });
            }
        },
        url: {
            type: String,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    comUrl: newVal
                });
            }
        },
        full_discount: {
            type: JSON,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    full_discount: newVal
                });
            }
        },
        payMoney: {
            type: Number,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    payMoney: newVal
                });
            }
        }
    },
    /**
   * 组件的初始数据
   */
    data: {
        comStyle: {
            ...defaultData
        },
        full_discount: {
            full_discount_info: {}
        }
    },
    /**
   * 组件的方法列表
   */
    methods: {
        processData() {
            var full_discount = this.data.full_discount;
            var tip = "";
            if (full_discount) {
                var full_discount_info = full_discount.full_discount_info;
                var full_discount_plus_info = full_discount.full_discount_plus_info;
                if (Object.keys(full_discount_info).length && !full_discount_plus_info) {
                    tip = "已满" + full_discount_info.refund_barrier + "元减" + full_discount_info.refund_limit + "元";
                } else if (Object.keys(full_discount_info).length && Object.keys(full_discount_plus_info).length) {
                    var diff = (full_discount_plus_info.refund_barrier - this.data.payMoney).toFixed(2);
                    tip = "已满" + full_discount_info.refund_barrier + "元减" + full_discount_info.refund_limit + "元,还差" + diff + "满" + full_discount_plus_info.refund_barrier + "减" + full_discount_plus_info.refund_limit + "元";
                } else if (!Object.keys(full_discount_info).length && Object.keys(full_discount_plus_info).length) {
                    var diff = (full_discount_plus_info.refund_barrier - this.data.payMoney).toFixed(2);
                    tip = "满" + full_discount_plus_info.refund_barrier + "元减" + full_discount_plus_info.refund_limit + "元," + "还差" + diff + "元";
                }
                this.setData({
                    tip: tip
                });
            }
        },
        cancelModal() {
            this.setData({
                isHidden: true
            });
            const myEventDetail = {
                goodsIsHidden: true
            };
            const myEventOptions = {};
            this.triggerEvent("myevent", myEventDetail, myEventOptions);
        },
        goTo(e) {
            wx.navigateTo({
                url: e.currentTarget.dataset.url
            });
        }
    },
    lifetimes: {
        ready() {
            let that = this;
            that.processData();
        }
    }
});